# -*- coding: utf-8 -*-
import scrapy

from scrapy_douban.items import ScrapyDoubanItem


class DoubanSpider(scrapy.Spider):
    name = 'douban'
    allowed_domains = ['douban.com']
    start_urls = ['http://douban.com/']

    def start_requests(self):
        for i in range(10):
            start_url = "https://movie.douban.com/top250?start={}".format(i * 25)
            yield scrapy.Request(start_url)

    def parse(self, response):
        movie_names = response.xpath(
            "//*[@id=\"content\"]/div/div[1]/ol/li/div/div[2]/div[1]/a/span[1]/text()").extract()
        movie_scores = response.xpath("//*[@class=\"rating_num\"]/text()").extract()
        year_country_types = response.xpath(
            "//*[@id=\"content\"]/div/div[1]/ol/li/div/div[2]/div[2]/p[1]/text()[2]").extract()
        movie_years = list()
        movie_countries = list()
        movie_types = list()

        for year_country_type in year_country_types:
            data = year_country_type.strip().replace("\xa0", "").split("/")
            year = ",".join(data[: -2])
            country = data[-2]
            t = data[-1]

            movie_years.append(year)
            movie_countries.append(country)
            movie_types.append(t)

        # 以 Json 形式推送
        # yield {
        #     "movie_name": movie_names,
        #     "movie_score": movie_scores,
        #     "movie_year": movie_years,
        #     "movie_country": movie_countries,
        #     "movie_type": movie_types
        # }

        with open("movie.txt", "w") as f:
            for movie_name, movie_score in zip(movie_names, movie_scores):
                f.writelines("电影: {}\t评分: {}\n".format(movie_name, movie_score))

        # 以 Item 形式推送
        item = ScrapyDoubanItem()
        for movie_name, movie_score, movie_year, movie_country, movie_type in zip(movie_names, movie_scores,
                                                                                  movie_years, movie_countries,
                                                                                  movie_types):
            item["name"] = movie_name
            item["score"] = movie_score
            item["year"] = movie_year
            item["country"] = movie_country
            item["type"] = movie_type
            yield item
